// import { asyncRoutes, constantRoutes } from '@/router'
import {
  constantRoutes
} from '@/router'
import Layout from '@/layout'

/**
 * Use meta.role to determine if the current user has permission
 * @param roles
 * @param route
 */
// 筛选router
function hasPermission(info, route, fatherPath = '') {
  // return info.some(item => item.url.indexOf(route.path) !== -1)
  if (info.url === (fatherPath + route.path)) {
    route.meta.title = info.title
  }
  return info.url === (fatherPath + route.path)
}

/**
 * Filter asynchronous routing tables by recursion
 * @param routes asyncRoutes
 * @param roles
 */
// 筛选router
export function filterAsyncRoutes(routes, list, fatherPath = '') {
  const res = []

  list.forEach((item, index) => {
    routes.forEach(route => {
      const tmp = {
        ...route
      }
      if (hasPermission(item, tmp, fatherPath)) {
        if (tmp.children) {
          tmp.children = filterAsyncRoutes(tmp.children, item.child, tmp.path + '/')
        }
        res.splice(index, 1, tmp)
      }
    })
  })
  return res
}
// 动态引入前端路径方法
const loadView = (view) => {
  return (resolve) => require([`@/views${view}`], resolve)
}
// 配置router
export function configAsyncRoutes(routes) {
  let res = []
  routes.forEach(item => {
    if (item.component === 'Layout') {
      item.component = Layout
      if (item.children && item.children.length > 0) {
        item.children.forEach(items => {
          items.component = loadView(items.component)
        //   if(items.children && items.children.length > 0){
        //     items.children.forEach(itemes => {
        //       itemes.component = loadView(itemes.component)

        // })
        //   }
        })
      }
    }
  })
  res = routes
  return res
}
const state = {
  routes: [],
  addRoutes: [],
  pageBtnRoute: [],
  btnRoute: []
}

const mutations = {
  SET_ROUTES: (state, routes) => {
    state.addRoutes = routes
    state.routes = constantRoutes.concat(routes)
  },
  SET_BTNROUTE: (state, pathList) => {
    state.pageBtnRoute = pathList
    state.btnRoute = pathList
  },
  MERGE_BTNROUTE: (state, pathList) => {
    state.btnRoute = state.pageBtnRoute.concat(pathList)
  }
}

const actions = {
  generateRoutes({
    commit
  }, list) {
    return new Promise(resolve => {
      let accessedRoutes
      // eslint-disable-next-line prefer-const
      // accessedRoutes = filterAsyncRoutes(asyncRoutes, list)
      // 404 page must be placed at the end !!!
      accessedRoutes.push({
        path: '*',
        redirect: '/401',
        hidden: true,
        children: []
      })
      commit('SET_ROUTES', accessedRoutes)
      resolve(accessedRoutes)
    })
  },
  generateBTNPermission({
    commit
  }, list) {
    commit('SET_BTNROUTE', list)
  },
  // 当前所使用的路径配置
  nowGenerateRoutes({
    commit
  }, list) {
    return new Promise(resolve => {
      let accessedRoutes
      // eslint-disable-next-line prefer-const
      accessedRoutes = configAsyncRoutes(list)
      // console.log(accessedRoutes)
      // 404 page must be placed at the end !!!
      // accessedRoutes.push({ path: '*', redirect: '/401', hidden: true, children: [] })
      commit('SET_ROUTES', accessedRoutes)
      resolve(accessedRoutes)
    })
  },
  // 获取按钮列表等权限
  getBtnPermission({
    commit
  }, list) {
    return new Promise(resolve => {
      let accessedButtons
      // eslint-disable-next-line prefer-const
      accessedButtons = configAsyncRoutes(list)
      commit('SET_BTNROUTE', accessedButtons)
      resolve(accessedButtons)
    })
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}
